package com.example.w.web.controller;

import com.example.w.common.dto.UserDTO;
import com.example.w.common.exception.SystemException;
import com.example.w.common.pojo.User;
import com.example.w.common.util.Result;
import com.example.w.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
@Api(tags = "用户管理接口")
public class UserController {
    @Autowired
    private UserService userService;

    /**
    注册用户
     */
    @PostMapping("register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        return new Result(true, "注册成功", userService.register(user));
    }

    /**
    登录
     */
    @PostMapping("login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody UserDTO userDTO){
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        userService.login(user);
        return new Result(true, "登录成功", null);
    }
    /**
     * 登出
     */
    @GetMapping("logout")
    @ApiOperation("用户登出")
    public Result logout() {
        userService.logout();
        return new Result(true, "登出成功", null);
    }
}
